﻿#include "u_chartview.h"

ChartView::ChartView(QChart *chart, QWidget *parent) : QChartView(chart, parent)
{
    this->setDragMode(QGraphicsView::RubberBandDrag);
    this->setMouseTracking(true);
    this->setStyleSheet("background: transparent");    //背景透明
}

QPoint ChartView::get_cur_mouse_pos()
{
    return beginPoint;
}

ChartView::~ChartView()
{

}

void ChartView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
          beginPoint=event->pos();
    QChartView::mousePressEvent(event);
}

void ChartView::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point =event->pos();
    emit mouseMovePoint(point);
    QChartView::mouseMoveEvent(event);
}

void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        endPoint=event->pos();
        QRectF rectf;
        rectf.setTopLeft(this->beginPoint);
        rectf.setBottomRight(this->endPoint);
        this->chart()->zoomIn(rectf);
    }
    else if(event->button() ==Qt::RightButton)
    {
        this->chart()->zoomReset();
    }

    QChartView::mouseReleaseEvent(event);
}

void ChartView::keyPressEvent(QKeyEvent *event)
{
    emit keyPressPoint(event);
    QChartView::keyPressEvent(event);
}
